import PubSub from 'pubsub-js';
import request from "../../../../utils/request";
import postrequest from "../../../../utils/postRequest";
// 获取全局实例
const appInstance = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listId:'',          //歌单id
        ids:'',             //id列表
        playlist:[],        //歌单详情列表
        index:'',
        t:2,                //记录收藏情况
        type:'',             //通信id
        globList:[],         //全局播放列表
        songlist:[]         //歌单音乐列表
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //初始化歌单列表id
       let {listId,playlist} = this.data
       listId = options.listid
       this.setData({
        listId
       })
       this.getDetail()         //获取歌单详情与id列表
       

       
    },

    //获取歌单的id列表
    async getDetail(){
        let {listId} = this.data
        let idList = []
        let res = await request('/playlist/detail',{id:listId})
        let  playlist = res.playlist
        // console.log(res)
        let  trackIds = playlist.trackIds          //id列表详情
        // console.log(trackIds.oftype)
        trackIds.forEach((item,index) => {
            let id = ''
            id = item.id
            idList.push(id)
        });
        let playcount = Math.floor(playlist.playCount/10000)
        let commentCount = playlist.commentCount
        if(playcount>100000){
          playcount = "9999+"
        }
        if( commentCount>100000 ){
          commentCount = '99999+'
        }
        // console.log(playcount)
        playlist.playCount = playcount
        playlist.commentCount = commentCount
        let ids = idList.toString()             //将id列表转为字符串
        // console.log(idList)
        // console.log(ids)
        this.setData({
            playlist,
            ids
        })
        this.getIdList()         //获取歌单的音乐的列表

    },


    //获取歌单的音乐的列表
    async getIdList(){
        let {ids} = this.data
        let res = await request('/song/detail', {ids: ids});
        let songlist = res.songs
        // console.log(songlist)
        this.setData({
          songlist
        })
    },


  // 跳转至songDetail页面
  toSongDetail(event){
    let {listId} = this.data
    let that = this;
    let globList=[];
    let index
    // if(event.currentTarget.dataset.index){
    //    index = event.currentTarget.dataset.index;
    // }else{
    //   index = 0
    // }
    index = event.currentTarget.dataset.index;
    let {songlist} =this.data
    this.setData({
      index,
      type:`playList`
    })
    //  console.log(this.data.type)
    songlist.forEach((item,index) => {
        let obj = {}
        obj.name = item.al.name;
        obj.artists= item.ar;
        obj.id = item.id
        globList.push(obj)
      });
      // console.log(globList)
  
      this.setData({
        globList
      })
  
      
      appInstance.globalData.playList = that.data.globList;
      appInstance.globalData.currentIndex = index;
      console.log(index)
      // console.log(songlist[index].id)
    // 路由跳转传参： query参数
    wx.navigateTo({
      // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
      // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
      url: `/pages/songPackage/pages/playmusic/playmusic?type=${this.data.type}&musicId=` + songlist[index].id,
      // success:function(){

      // }
    },

    )
  },

    // playall(){

    // },

     async SouChan(){
      //  console.log("sdfsdfsdfsdf")
      let {listId,t} = this.data
      let res
      if(t===2){
        res = await request('/playlist/subscribe',{t:1,id:listId})
        t=1
      }else{
        let res = await request('/playlist/subscribe',{id:listId,t:2})
        t=2
      }
     
      console.log(res)
      console.log(t)
      this.setData({
        t
      })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})